#!/usr/bin/python2.4
#
# Copyright 2007 Dobromir Montauk. All Rights Reserved.

"""Test cases for dashboard.py
"""

__author__ = "dmontauk@gmail.com (Dobromir Montauk)"

import unittest
import datetime

import pygooglechart
from dashomatic import BadDataError,BadChartError
from dashomatic import dashboard
from dashomatic import chart

class Dashboard_test(unittest.TestCase):
  def setUp(self):
    self.simple_dict = [{ 1 : "a",
                          2 : "b",
                          3 : "c",
                          4 : 1,
                          5 : 0.0,
                          },
                        { 1 : "d",
                          2 : "e",
                          3 : "f",
                          4 : 2,
                          5 : 0.0,
                          },
                        { 1 : "g",
                          2 : "h",
                          3 : "i",
                          4: 3,
                          5: 0.0,
                          },
                        ]
    self.simple_list = [ [1,2,3,4,5],
                         ["a","b","c",1,0.0],
                         ["d","e","f",2,0.0],
                         ["g","h","i",3,0.0],
                        ]
    self.simple_dict_dash = dashboard.Dashboard()
    self.simple_dict_dash.AddData("simple",self.simple_dict)
    
  def testBuildSimpleDashboard(self):
    self.simple_dict_dash.AddChart("simple",4,{"type":"SimpleLineChart"})
    html = self.simple_dict_dash.BuildDashboard("Hello world")
    self.assertTrue("<h2>simple</h2>" in html)  # We need to check for dash!
    # TODO(dmontauk): We need to make sure the charts were added, too.
    
  def testBuildSimpleDashboardNewTemplate(self):
    pass
    # Need to test what happens when we add our own template!
    
  def testBuildSimpleCsvNoArgs(self):
    csv = self.simple_dict_dash.BuildCsv()
    expected_csv = "1,2,3,4,5\r\na,b,c,1,0.0\r\nd,e,f,2,0.0\r\ng,h,i,3,0.0\r\n"
    self.assertEquals(csv,expected_csv)
    
  def testBuildSimpleCsvArgs(self):
    extra_data = [["x","y","z"],
                  [ 1 , 2 , 3 ],
                  [ 4 , 5 , 6 ],
                  [ 7 , 8 , 9 ]]
    self.simple_dict_dash.AddData("extra",extra_data)
    csv = self.simple_dict_dash.BuildCsv(["extra"])
    expected_csv = "x,y,z\r\n1,2,3\r\n4,5,6\r\n7,8,9\r\n"
    self.assertEquals(csv,expected_csv)
    
  def testBuildSimpleCsvMultiple(self):
    extra_data = [["x","y","z"],
                  [ 1 , 2 , 3 ],
                  [ 4 , 5 , 6 ],
                  [ 7 , 8 , 9 ]]
    self.simple_dict_dash.AddData("extra",extra_data)
    csv = self.simple_dict_dash.BuildCsv()
    expected_csv =("1,2,3,4,5\r\na,b,c,1,0.0\r\nd,e,f,2,0.0\r\ng,h,i,3,0.0\r\n"
                    "x,y,z\r\n1,2,3\r\n4,5,6\r\n7,8,9\r\n")
    self.assertEquals(csv,expected_csv)
   
  def testBuildMultipleDashboard(self):
    extra_data = [ ['x','y','z'],
                   [100,20,300],
                   [30,400,20],
                   [100,0,0],
                   [655,345,234],
                  ]
    self.simple_dict_dash.AddData("extra",extra_data)
    for col in ['x','y','z']:
      self.simple_dict_dash.AddChart("extra",col,{"type":"SimpleLineChart"})
    html = self.simple_dict_dash.BuildDashboard("Hello world")
    self.assertTrue("<h2>simple</h2>" in html)
    self.assertTrue("<h2>extra</h2>" in html)
    # TODO(dmontauk): We need to make sure the charts were added, too.
    
  def testAddDataListOfLists(self):
    self.simple_dict_dash.AddData("simple_list",self.simple_list)
    self.assertTrue(self.simple_dict_dash.data.has_key("simple_list"))
  
  def testAddDataListOfDicts(self):
    self.simple_dict_dash.AddData("simple_dict",self.simple_dict)
    self.assertTrue(self.simple_dict_dash.data.has_key("simple_dict"))

  def testAddDataObject(self):
    data = dashboard.Data(self.simple_list)
    self.simple_dict_dash.AddData("simple_obj",data)
    self.assertTrue(self.simple_dict_dash.data.has_key("simple_obj"))
    
  def testGetColumn(self):
    col = self.simple_dict_dash.GetColumn("simple",2)
    self.assertEquals(col,['c','f','i'])
  
  def testGetColumnBadIndex(self):
    self.assertRaises(BadDataError,self.simple_dict_dash.GetColumn,"simple",100)
    
  def testGetColumnBadDataSource(self):
    self.assertRaises(BadDataError,self.simple_dict_dash.GetColumn,"simplea",1)
    
  def testAddChartBadDataSource(self):
    c = pygooglechart.SimpleLineChart(400,200)
    self.assertRaises(BadDataError,self.simple_dict_dash.AddChart,"simplea",1,c)

  def testAddChartBadColumn(self):
    c = pygooglechart.SimpleLineChart(400,200)
    self.assertRaises(BadDataError,self.simple_dict_dash.AddChart,"simple",10,c)
   
  def testAddChart(self):
    chart = pygooglechart.SimpleLineChart(400,200)
    self.simple_dict_dash.AddChart("simple",4,chart)
    expected_url = ('http://chart.apis.google.com/chart?cht=lc&chs=400x200'
                    '&chd=t:33.3,66.7,100.0&chtt=4&chxt=x,y&chxl=0:|a|d|g'
                    '&chxr=1,0,3')
    self.assertEquals(self.simple_dict_dash.charts["simple"][4].get_url(),
                      expected_url)

  def testAddChartZeros(self):
    chart = pygooglechart.SimpleLineChart(400,200)
    self.simple_dict_dash.AddChart("simple",5,chart)
    expected_url = ('http://chart.apis.google.com/chart?cht=lc&chs=400x200'
                    '&chd=s:AAA&chtt=5&chxt=x,y&chxl=0:|a|d|g&chxr=1,0,0.0')
    self.assertEquals(self.simple_dict_dash.charts["simple"][5].get_url(),
                      expected_url)

  def testAddChartFromDict(self):
    d = { 'type'  : 'SimpleLineChart',
          'height': 200,
          'width' : 400,
          'legend': ["FirstDataSet"],
          'colors' : ['208020', '202080']
        }
    self.simple_dict_dash.AddChart("simple",4,d)
    expected_url = ('http://chart.apis.google.com/chart?cht=lc&chs=400x200'
                    '&chd=t:33.3,66.7,100.0&chtt=4&chdl=FirstDataSet'
                    '&chco=208020,202080&chxt=x,y&chxl=0:|a|d|g&chxr=1,0,3')
    self.assertEquals(expected_url,
                      self.simple_dict_dash.charts['simple'][4].get_url())  
    
  def testDictToChartNoType(self):
    self.assertRaises(BadChartError,self.simple_dict_dash.DictToChart,
                      {"width":50,"height":50})
  
  def testDictToChartBadType(self):
    self.assertRaises(BadChartError,self.simple_dict_dash.DictToChart,
                      {'type':"NoSuchChartType"})

  def testDictToChartDefaults(self):
    chart = self.simple_dict_dash.DictToChart({'type':"SimpleLineChart"})
    expected_url ="http://chart.apis.google.com/chart?cht=lc&chs=400x200&chd=s:"
    self.assertEquals(expected_url,chart.get_url())
  
  def testDictToChartAllOptionsGood(self):
    d = { 'type'  : 'SimpleLineChart',
          'height': 200,
          'width' : 400,
          'legend': ["FirstDataSet","SecondDataSet"],
          'colors' : ['208020', '202080']
        }
    chart = self.simple_dict_dash.DictToChart(d)
    chart.add_data([1,2,3,4])
    chart.add_data([2,3,4,5])
    expected_url = ("http://chart.apis.google.com/chart?cht=lc&chs=400x200"
                    "&chd=s:BCDE,CDEF&chdl=FirstDataSet|SecondDataSet"
                    "&chco=208020,202080")
    self.assertEquals(expected_url,chart.get_url())

  def testDictToChartBadLegend(self):
    d = { 'type'  : 'SimpleLineChart',
          'height': 200,
          'width' : 400,
          'legend': "hello world",
          'colors' : ['208020', '202080']
        }
    self.assertRaises(BadChartError,self.simple_dict_dash.DictToChart,d)

  def testDictToChartBadColors(self):
    d = { 'type'  : 'SimpleLineChart',
          'height': 200,
          'width' : 400,
          'legend': ["one","two"],
          'colors' : 208020,
        }
    self.assertRaises(BadChartError,self.simple_dict_dash.DictToChart,d)

  def testDictToChartIntLabels(self):
    pass    # We should do this one day :(
  
class Data_test(unittest.TestCase):
  def setUp(self):
    self.simple_dict = [{ 1 : "a",
                          2 : "b",
                          3 : "c",
                          4 : 1,
                          5 : 0.0,
                          },
                        { 1 : "d",
                          2 : "e",
                          3 : "f",
                          4 : 2,
                          5 : 0.0,
                          },
                        { 1 : "g",
                          2 : "h",
                          3 : "i",
                          4: 3,
                          5: 0.0,
                          },
                        ]
    self.simple_list = [ [1,2,3,4,5],
                         ["a","b","c",1,0.0],
                         ["d","e","f",2,0.0],
                         ["g","h","i",3,0.0],
                        ]
    self.data = dashboard.Data(self.simple_list)

  def testProcessRawData(self):
    self.assertEquals(dashboard.Data.ProcessRawData(self.simple_dict,
                                                          None,""),
                      dashboard.Data.ProcessRawData(self.simple_list,
                                                          None,"")
                      )
  def testProcessRawDataEmpty(self):
    self.assertRaises(BadDataError,dashboard.Data.ProcessRawData,
                      [],None,"")

  def testGetColumn(self):
    data = self.data.GetColumn(1)
    expected_data = ["b","e","h"]
    self.assertEquals(data,expected_data)
                      


    
if __name__ == '__main__':
  tests = []
  tests.append(unittest.TestLoader().loadTestsFromTestCase(Dashboard_test))
  tests.append(unittest.TestLoader().loadTestsFromTestCase(Data_test))
  all_tests = unittest.TestSuite(tests)
  unittest.TextTestRunner(verbosity=2).run(all_tests)  
